import Vue from 'vue'
// 导入路由组件
import VueRouter from 'vue-router'
// 导入login组件
import Login from '../components/login.vue'
// 导入home组件
import Home from '../components/home1.vue'
// 导入home子组件为weicome
import Welcome from '../components/welcome.vue'
// 导入 users 组件
import Users from '../components/users/users2.vue'
// 导入权限管理 组件
import Right from '../components/power/right.vue'
// 导入角色列表 组件
import Roles from '../components/power/roles1.vue'
// 导入 商品组件
import Cate from '../components/goods/cate.vue'
// 导入 parmas组件
import Parmas from '../components/goods/parmas1.vue'
import List from '../components/goods/List.vue'
import add from '../components/goods/add.vue'
import order from '../components/Order/Order.vue'

// 注册全局路由
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome', component: Welcome
      },
      {
        path: '/users', component: Users
      },
      {
        path: '/rights', component: Right
      },
      {
        path: '/roles', component: Roles
      },
      {
        path: '/categories', component: Cate
      },
      {
        path: '/params', component: Parmas
      },
      {
        path: '/goods', component: List
      },
      {
        path: '/goods/add', component: add
      },
      {
        path: '/orders', component: order
      }

    ]
  }
]
// 创建路由表对象
const router = new VueRouter({
  routes
})

// 设置路由守卫函数
// 作用 在浏览器地址栏的hash值 发生改变时触发, 可以用来检查 当前访问的 hash路径 是否需要有token
router.beforeEach((to, from, next) => {
  // 判断 访问的hash地址是否为login 如果为login ,则放行
  if (to.path === '/login') next()
  // 如果不为login,尝试获取本地存储中的token
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有login值, 则强制跳转到login
  if (!tokenStr) return next('/login')
  // 如果有 则放行
  next()
})

export default router
